<template>
<div class="contain">
  <div class="loader-base" v-if="showLoad"><div class="loader-wrap"></div></div>
  <header class="hd-top">
    <span class="iconfont icon-left" @click="goBack"></span>
    <h2 class="title">选择{{type='1'?'发':'收'}}货地址</h2>
    <button class="btn" @click="add">新增</button>
  </header>
  <div class="page">
    <div class="address-list" v-for="item in todolist">
      <dl @click="checkAddress(item)">
       <dt class="ft17">{{item.Contacts}}  {{item.ContactsTel}}</dt>
       <dd class="ft14">{{item.Province | province}}{{item.City | city}}{{item.District | district}}{{item.Address}} </dd>
       <dd class="ft14" v-if="item.SterilizeTime">消毒时间：{{item.SterilizeTime}}</dd>
      </dl>
      <div class="addr-foot cf">
        <label @change="checkAddress(item, 'save')">
          <input type="radio" class="list-radio-check" name="address" v-if="item.IsChecked=='Y'" checked>
          <input type="radio" class="list-radio-check" name="address" v-if="item.IsChecked=='N'" >
          <span class="list-radio-circle"></span>
          <em class="txt">默认地址</em>
        </label>
        <div class="f-r">
          <span class="mr20">
            <i class="iconfont icon-order-entry" @click="EditAddress(item.Guid)"></i>编辑</span>
          <span>
            <i class="iconfont icon-delete2" @click="DelAddress(item.Guid)"></i>删除</span>
        </div>
      </div>
    </div>
    <div class="noDate_page" v-if="todolist.length<=0">
      <div class="noDate_icon"><i class="iconfont icon-null"></i></div>
      未找到相关数据
    </div>
  </div>
  <div @click="add" class="btn-bottom"><div class="btn">新增</div></div>
</div>
</template>

<script>
export default {
  name:'addressList',
  data() {
    return {
      todolist: [],
      seleAddr: "",
      showLoad: false,
    };
  },
  computed: {
    userinfo() {
      if (localStorage.UserInfo) {
        return JSON.parse(localStorage.UserInfo);
      }
    },
    types(){
      return this.$route.query.type;
    },
    mine(){
      if(this.$route.query.mine){
        return true;
      }else{
        return false;
      }
    }
  },
  created() {
    this.getData();
    if(this.mine && this.types == '1'){
      document.title = '发货地址';
    };
    if(this.mine && this.types == '2'){
      document.title = '收货地址';
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    add(){
      var query = this.$route.query;
      query.type=this.types;
      if(this.mine){
        query.mine=true;
        // query = {
        //   type: this.types,
        //   mine:true
        // }
      }else{
          query.type=this.types;
        // query = {
        //   type: this.types
        // }
      };
      this.$router.push({
        path: "/addressForm",
        query: query
      });
    },
    checkAddress(item, operate) {
      var self = this;
      self.showLoad = true;
      if(this.types=="1"){
       sessionStorage.FHAddress =''
        sessionStorage.FHAddress = JSON.stringify(item);
        console.log(sessionStorage.FHAddress);
      }else if(this.types=="2"){
          sessionStorage.SHAddress =''
        sessionStorage.SHAddress = JSON.stringify(item);
      };
      if(operate == 'save'){
        var query = {
          Guid: item.Guid,
          type: item.type,
          LoginedStaffID: this.userinfo.StaffID,
          LoginedName: this.userinfo.Name
        };
        
        this.Ajax.post("SetDefaultCustomerAddress", query).then(function(resp) {
          self.showLoad = false;
          if (resp.IsSuccess) {
            //跳转地址判断
            if(self.mine){
              self.$router.push({
                path: "/user"
              });
            }else{
              self.$router.push({
                path: "/onlineOrder",
                query: self.$route.query
              });
            };
          } else {
            self.$vux.toast({
              type:'warn',
              text:resp.ErrorMessage,  
            });
          }
        });
      } else {
        self.showLoad = false;
        //跳转地址判断
        if(!self.mine){
          self.$router.push({
            path: "/onlineOrder",
            query: self.$route.query
          });
        };
      };
    },
    getData() {
      var _this = this;
      _this.showLoad=true;
      var query = {
        CreateUserId: this.userinfo.StaffID,
        type: this.types,
        PageIndex:1,
        PageSize:100
      };
      
      this.Ajax.post("SearchCustomerAddress", query).then(function(resp) {
        _this.showLoad = false;
        _this.todolist = resp.Datas;
      });
    },
    DelAddress(id) {
      var self = this;
      this.$vux.confirm.show({
        title:'提示',
        content: '确认删除地址吗',
        onConfirm () {
          self.showLoad = true;
          if(!self.mine){
            var addressGuid = '';
            if(self.types=="1" && sessionStorage.FHAddress != null && sessionStorage.FHAddress != ''){
              addressGuid = JSON.parse(sessionStorage.FHAddress).Guid;
            }else if(self.types=="2" && sessionStorage.SHAddress != null && sessionStorage.FHAddress != ''){
              addressGuid = JSON.parse(sessionStorage.SHAddress).Guid;
            };
            if(addressGuid == id){
              if(self.types=="1"){
                sessionStorage.removeItem('FHAddress')
              }else if(self.types=="2"){
                sessionStorage.removeItem('SHAddress')
              }
            }
          };
          var query = {
            Guid: id
          };
          self.Ajax.post("DelCustomerAddress", query).then(function(resp) {
            if (resp.IsSuccess) {
              self.$vux.toast.show({
                type: "success", //success, warn, cancel, text
                text: "删除成功"
              });
              self.getData();
              self.showLoad = false;
            }
          });
        }
      });
    },
    EditAddress(id) {
      var query = this.$route.query;
      query.AddressGuid =id;
      query.type =this.types;
      if(this.mine){
        query.mine = true;
      };
      this.$router.push({
        path: "/addressForm",
        query: query
      });
    }
  }
};
</script>